package com.huadao.cart_ccs.common.deviceController;

import com.huadao.cart_ccs.experiment.paramsSetting.ParamConsts;

import java.util.Date;

public abstract class BaseEnvironmentController extends BaseController  {
    //当前值
    protected double currentValue;
    //设定的值
    protected Double settedValue;

    //是否激活下位机设备工作
    protected boolean isActivated;
    //读取更新的时间
    protected Date updateReadTime;

    //判断当前值是否在设定值的误差范围之内
    public boolean isInControl(){
        if(settedValue==null){
            return true;
        }
        //数值在误差范围内
        if((currentValue+getTolerance()>settedValue)&&(currentValue-getTolerance()<settedValue)){
            return true;
        }
        return false;
    }

    //长时间未读到数值,当前值过期
    public boolean isCurrentValueExpired(){
        if(updateReadTime==null){
            return true;
        }
        Date now=new Date();
        int timeDiff= (int) ((now.getTime()-updateReadTime.getTime())/1000);
        if(timeDiff>ParamConsts.sendEnvironmentFrenquency){
            return true;
        }else {
            return false;
        }
    }

    public double getCurrentValue() {
        return currentValue;
    }

    public void setCurrentValue(double currentValue) {
        this.currentValue = currentValue;
        //更新数据时，刷新更新时间
        updateReadTime=new Date();
    }

    public Double getSettedValue() {
        return settedValue;
    }

    public void setSettedValue(Double settedValue) {
        this.settedValue = settedValue;
    }

    public abstract double getTolerance();

    public boolean isActivated() {
        return isActivated;
    }

    public void setActivated(boolean activated) {
        isActivated = activated;
    }

    public Date getUpdateReadTime() {
        return updateReadTime;
    }

    public void setUpdateReadTime(Date updateReadTime) {
        this.updateReadTime = updateReadTime;
    }
}
